// 给我留言页面
<template>
  <div id="message">
    <div class="title"> </div>
    <div class="wrapper">
      <div class="wrapper-input">
        <div class="tip">
          <p>提示：请注意您的言论，网络并非法外之地</p>
        </div>
        <div class="content">
          <span style="font-size: 26px ;display: flex;justify-content: center;">输入区</span>
          <div class="title1">
            <el-input class="input"  placeholder="讨论题目" v-model="title" clearable>
            </el-input>
          </div>
          <el-input class="input" type="textarea" :rows="8"  placeholder="讨论内容" v-model="content" clearable>
          </el-input>
          <div class="btn">
            <el-button style="width: 200px;" type="warning" @click="submit()">提交</el-button>
          </div>
        </div>
        
      </div>
      <div class="all">
        <ul class="msglist">
          <li class="list" @mouseenter="enter(index)" @mouseleave="leave(index)" v-for="(data,index) in msg"
            :key="index">
            <p class="title"> <i class="iconfont icon-untitled33"></i>{{data.title}}</p>
            <p class="content">{{data.content}}</p>
            <p class="date"><i class="iconfont icon-date"></i>{{data.time}}</p>
            <div v-for="(replayData,index2) in data.replays" :key="index2">
              <p class="comment"><i class="iconfont icon-huifuxiaoxi"></i>{{replayData.replay}}</p>
            </div>
            <span class="replay" @click="replay(data.id)" v-if="flag && index == current">回复</span>
          </li>
        </ul>
      </div>
      <div class="pagination">
        <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
          :current-page="pagination.current" :page-sizes="[4,6,8,10]" :page-size="pagination.size"
          layout="total, sizes, prev, pager, next, jumper" :total="pagination.total">
        </el-pagination>
      </div>
    </div>
  </div>
</template>

<script>
  export default {
    // name: 'message'
    data() {
      return {
        flag: false,
        current: 0,
        title: "",
        content: "",
        pagination: { //分页后的留言列表
          current: 1, //当前页
          total: null, //记录条数
          size: 4 //每页条数
        },
        msg: []
      }
    },
    created() {
      this.getMsg()
    },
    // watch: {

    // },
    methods: {
      getMsg() {
        this.$axios(`/api/messages/${this.pagination.current}/${this.pagination.size}`).then(res => {
          let status = res.data.code
          if (status == 200) {
            this.msg = res.data.data.records
            this.pagination = res.data.data
          }
        })
      },
      //改变当前记录条数
      handleSizeChange(val) {
        this.pagination.size = val
        this.getMsg()
      },
      //改变当前页码，重新发送请求
      handleCurrentChange(val) {
        this.pagination.current = val
        this.getMsg()
      },
      // formatTime(date) { //日期格式化
      //   let year = date.getFullYear()
      //   let month= date.getMonth()+ 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1;
      //   let day=date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
      //   let hours=date.getHours() < 10 ? "0" + date.getHours() : date.getHours();
      //   let minutes=date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes();
      //   let seconds=date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
      //   // 拼接
      //   return year+"-"+month+"-"+day+" "+hours+":"+minutes+":"+seconds;
      // },
      submit() {
        let date = new Date()
        if (this.title.length == 0 || this.content.length == 0) { //非空判断
          this.$message({
            type: 'error',
            message: '留言标题或内容不能为空',
          })
        } else {
          this.$axios({
            url: "/api/message",
            method: "post",
            data: {
              title: this.title,
              content: this.content,
              time: date
            }
          }).then(res => {
            let code = res.data.code
            if (code == 200) {
              this.$message({
                type: "success",
                message: "留言成功"
              })
            }
            this.getMsg()
          })
        }
        this.title = ""
        this.content = ""
        this.getMsg()
      },
      replay(messageId) { //回复留言功能
        this.$prompt('回复留言', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          inputPattern: /^[\s\S]*.*[^\s][\s\S]*$/,
          inputErrorMessage: '回复不能为空'
        }).then(({ value }) => {
          let date = new Date()
          console.log(messageId)
          this.$axios({
            url: '/api/replay',
            method: 'post',
            data: {
              replay: value,
              replayTime: date,
              messageId: messageId
            }
          }).then(res => {
            this.getMsg()
          })
          this.$message({
            type: 'success',
            message: '回复成功'
          });
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '取消输入'
          });
        });
      },
      enter(index) {
        this.flag = true
        this.current = index
      },
      leave(index) {
        this.flag = false;
        this.current = index;
      }
    }
  }
</script>

<style lang="less" scoped>
  .pagination {
    display: flex;
    justify-content: center;
  }

  #message {
    width: 980px;
    margin: 0 auto;
  }

  .title {
    margin: 20px;
  }

  .content {
    padding: 20px 0px;
  }

  #message {
    opacity: 0.8;

    /* 设置透明度为 0.7，即 70% 不透明 */
    .btn {
      padding-bottom: 20px;
      position: relative;
      left: 150px;
      top: 10px;
      min-width: 100px;
    }

    .all {
      .date {
        color: rgb(80, 157, 202);
        line-height: 45px;
        font-size: 13px;
      }

      .list {
        background-color: #eee;
        padding: 10px;
        border-radius: 4px;
        margin: 10px 0px;
        position: relative;
        transition: all .3s ease;

        .title {
          color: #5f5f5f;
          margin: 0px;
          font-size: 13px;
          line-height: 30px;
        }

        .content {
          padding: 0px;
        }

        .icon-untitled33 {
          font-size: 13px;
          margin-right: 4px;
        }

        .icon-date {
          font-size: 13px;
          margin-right: 4px;
          color: rgb(80, 157, 202);
        }

        .replay {
          position: absolute;
          right: 30px;
          bottom: 15px;
          color: tomato;
          cursor: pointer;
          transition: all .3s ease;
        }

        .comment {
          margin: -7px 0px;
          padding-bottom: 12px;
          font-size: 13px;
          color: #28b2b4;

          i {
            margin-right: 4px;
          }
        }
      }
    }
  }

  #message .wrapper {
    background-color: #eee;
    padding: 20px;

  }

  .wrapper-input {
    display: flex;
    justify-content: space-between;
    /* 让子元素左右排列 */
    align-items: center;
    /* 垂直居中对齐子元素 */
    margin: 20px 0;
  }

  .input{
    width: 500px;margin: 10px 0; 
    border: 1px solid red;
  }

  .search-title {
    flex: 1;
    /* 占据剩余空间 */
    margin-right: 10px;
    /* 设置子元素之间的间距 */
    font-size: 20px;
    font-weight: bold;
  }

  .tip{
    width: 320px;
    font-size: 26px;
    font-weight: bold;
    color: red;
  }

  .tip p{
    position: relative;
    top: -60px;
  }
</style>